package com.lanou3g.musicenjoy.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.Adapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.entity.Music;
import com.lanou3g.musicenjoy.entity.NewSongListEntity;
import com.lanou3g.musicenjoy.entity.PlayMusicEntity;
import com.lanou3g.musicenjoy.eventbus.MyEvent;
import com.lanou3g.musicenjoy.volley.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

public class MusicRecycleViewAdapter extends RecyclerView.Adapter<MusicRecycleViewAdapter.MyViewHolder> {
    private Context mContext;
    private List<Music.SonglistEntity> datas;
    private int clickPosition;//记录当前点击的位置
    private List<PlayMusicEntity> data = new ArrayList<>();
    private int numOfData;//data的总长度
    private int numOfNow;//已经完成的长度
    private RequestQueue requestQueue;


    public MusicRecycleViewAdapter(Context c, List<Music.SonglistEntity> datas) {
        mContext = c;
        this.datas = datas;
        requestQueue = VolleySingleton.getInstance(mContext).getRequestQueue();

    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout musicLinearLayout;
        private TextView textView;
        private TextView getTextView;
        private TextView num;

        public MyViewHolder(View view) {
            super(view);
            textView = (TextView) view.findViewById(R.id.itemTextView);
            getTextView = (TextView) view.findViewById(R.id.musicTextView);
            musicLinearLayout = (LinearLayout) view.findViewById(R.id.musicLinearLayout);
            num= (TextView) view.findViewById(R.id.num);

        }
    }

    @Override
    public MusicRecycleViewAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_music_item, null);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MusicRecycleViewAdapter.MyViewHolder holder, final int position) {
        Music.SonglistEntity entity = datas.get(position);
        holder.textView.setText(entity.getTitle());
        holder.getTextView.setText(entity.getAuthor());
        holder.num.setText(position+1+". ");
        holder.musicLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("Ffrfffff", "你点击了 ");
                getAllUrl(position);
            }
        });

    }

    private void getAllUrl(int position) {
        clickPosition = position;//记录下当前点击的position
        numOfData = datas.size();//设置总长度
        numOfNow = 0;//设置当前完成的长度
        for (int i = 0; i < datas.size(); i++) {//获得所有的Url进行网络数据的拉取
            String url = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0&method=baidu.ting.song.getInfos&format=json&songid=" +
                    datas.get(i).getSong_id() + "&ts=1445399459391&e=SmJtjLwgnuWRzBbIIHeXgNusY1L" +
                    "%2FXZoWyOitaQ3ExdPXWpSPZ0Fras1q%2FLVxivZr&nw=2&fr=sns&ucf=1&res=1&l2p=2943&lpb=&usup=1&lebo=0";
            getInternetData(url, i);//i记录了当前正在解析哪个URL
            Log.i("fffffffffff", url);
            //让data集合拥有跟数据匹配的长度
            PlayMusicEntity playMusicEntity = new PlayMusicEntity();
            data.add(playMusicEntity);
        }

    }

    //网络解析
    private void getInternetData(String url, final int position) {

        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {//成功回调
                //进行解析
                NewSongListEntity entity = JSON.parseObject(s, NewSongListEntity.class);
                if (entity != null) {//解析成功,这是其中一个音乐的播放地址

                    String url = entity.getSongurl().getUrl().get(0).getShow_link();//获得了URL

                    String pic = datas.get(position).getPic_big();
                    String singerName = datas.get(position).getAuthor();
                    String songName = datas.get(position).getTitle();

                    //设置data的相应位置的数据
                    data.get(position).setUrl(url);
                    data.get(position).setPicUrl(pic);
                    data.get(position).setSingerName(singerName);
                    data.get(position).setSongName(songName);
                    Log.i("NewSongListFragment", "getURl->position" + position);

                    numOfNow++;
                    Log.i("NewSongListFragment", "当前已完成" + numOfNow);

                    if (numOfNow >= numOfData) {
                        //所有数据均已加载完成
                        EventBus.getDefault().post(new MyEvent.PassData(data, clickPosition));
                    }

                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                numOfNow++;
                Log.i("NewSongListFragment", "当前已完成" + numOfNow);

            }
        });
        requestQueue.add(request);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }


}
